package net.pm.core.web.user;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import net.pm.core.dateEdtor.DateEditor_Long;
import net.pm.core.entities.db.Loginlog;
import net.pm.core.entities.db.Pmuser;
import net.pm.core.service.pmuser.LoginLogServicer;
import net.pm.core.service.pmuser.PMUserServicer;
import net.pm.core.web.BaseController;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;


@Controller
@Scope("request")
@RequestMapping("/loginlog/")
public class LoginLogController extends BaseController{
	
	@Resource(name="DateEditor_Long")
	private DateEditor_Long dateEditor_Long;
	
	@Resource(name="LoginLogServicer")
	private LoginLogServicer loginLogServicer;
	
	@Resource(name="PMUserServicer")
	private PMUserServicer pmUserServicer;
	
	
	
	/**
	 * 去到登录日志页面
	 * @return
	 */
	@RequestMapping(value="manage",method=RequestMethod.GET)
	public ModelAndView manage() {

		ModelAndView mav = new ModelAndView("loginlog/loginlog_manage");
		Integer deptId = super.obtainCurrentDept().getId();
		List<Pmuser> users = pmUserServicer.findUsersByDeptId(deptId);

		mav.addObject("users", users);
		mav.addObject("logResultMap", LoginLogServicer.logResultMap);
		mav.addObject("this", super.useStaticPacker(this.getClass().getName()));
		mav.addObject("Loginlog", super.useStaticPacker(Loginlog.class.getName()));
		return mav;
	}
	
	/**
	 * 获取订单对应的状态的字符串
	 * @param status
	 * @return
	 */
	public static String obtainLogResultStr(Short logResultKey){
		String[] statusStrs = {"帐户被禁","密码错误","登录成功","默认显示","全部"};
		return statusStrs[logResultKey];
	}
	
	/**
	 * 自动转到默认列表
	 * 
	 * @return
	 * */
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public ModelAndView list() {
		return list(null, null, null, Loginlog.LOGRESULT_DEFAULT_DISPLAY);
	}

	/**
	 * 搜索相关的登录日志
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public ModelAndView list(Date startdate, Date enddate, Long userId,
			Short logResultKey) {
		ModelAndView mav = new ModelAndView("loginlog/loginlog_list");
		Integer deptId = super.obtainCurrentDept().getId();
		List<Loginlog> loginlogs = loginLogServicer.findLoginlogs(startdate,
				enddate, deptId, userId, logResultKey);
		mav.addObject("loginlogs", loginlogs);
		mav.addObject("this", super.useStaticPacker(this.getClass().getName()));
		mav.addObject("Loginlog", super.useStaticPacker(Loginlog.class.getName()));
		
		return mav;
	}
	
	
	/**
	 * 数据绑定
	 * @param binder
	 */
	@InitBinder
	public void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, dateEditor_Long);
	}
	
	
}
